/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cpservice;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import vn.webapp.controller.BaseRest;
import vn.webapp.dto.Response;
import vn.webapp.model.Classing;
import vn.webapp.model.CourseTeacherClassRaw;
import vn.webapp.model.Coursing;
import vn.webapp.service.ClassesService;
import vn.webapp.service.ClassingService;
import vn.webapp.service.CoursingService;

/**
 *
 * @author Tonytran
 */
@Controller("cpServiceCoursing")
@RequestMapping(value = {"/cpservice"})
public class CoursingController extends BaseRest {

    @Autowired
    private CoursingService coursingService;
    
    @Autowired
    private ClassingService classingService;
    
    @Autowired
    private ClassesService classesService;

    @ResponseBody
    @RequestMapping(value = "delcoursing", method = RequestMethod.POST)
    public Response delCoursing(@RequestParam(value = "id", defaultValue = "0") int id) {
        int res = coursingService.removeACoursing(id);
        if (res > 0) {
            return new Response(true, "Delete successfully.", res);
        } else {
            return new Response(false, "Delete unsuccessfully. Please try again.", res);
        }
    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/addacoursing", method = RequestMethod.POST)
    @ResponseBody
    public Response addACoursing(
            @RequestParam(value = "teacherId", defaultValue = "0") int teacherId,
            @RequestParam(value = "courseId", defaultValue = "0") int courseId,
            @RequestParam(value = "sessionId", defaultValue = "0") int sessionId,
            @RequestParam(value = "classId", defaultValue = "0") int[] a_classIds) {
        
       if(a_classIds.length == 0)
       {
           return new Response(false, "Please choose at least 1 class.", 0);
       }
       Coursing o_Exists = coursingService.checkExists(0, teacherId, courseId, sessionId);
       
       if(o_Exists == null){
            // Return data back to ajax calling
            int coursingId = coursingService.saveACoursing(teacherId, courseId, sessionId);
            int classId = 0;
            ArrayList<Integer> aClassIds = new ArrayList<Integer>();
            int iInteration = 0;
            if(coursingId > 0){
                for (int i_ClassId : a_classIds) {
                    classId = classingService.saveAClassing(coursingId, i_ClassId); 
                    aClassIds.add(classId);
                    iInteration++;
                }
            }
            if(iInteration > 0 && iInteration == aClassIds.size())
                return new Response(true, "Save successfully.", 1);
            else 
                return new Response(false, "Save unsuccessfully. Please retry", 0);
       }else
       {
           return new Response(false, "This course exists. Please choose another one.",0);
       }
    }
    
    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/coursingdetail", method = RequestMethod.POST)
    @ResponseBody
    public Response editACoursing(
            @RequestParam(value = "id", defaultValue = "0") int id,
            @RequestParam(value = "teacherId", defaultValue = "0") int teacherId,
            @RequestParam(value = "courseId", defaultValue = "0") int courseId,
            @RequestParam(value = "sessionId", defaultValue = "0") int sessionId,
            @RequestParam(value = "classId", defaultValue = "0") int[] a_classIds) {
        
       if(a_classIds.length == 0)
       {
           return new Response(false, "Please choose at least 1 class.", 0);
       }
       Coursing o_Exists = coursingService.checkExists(id, teacherId, courseId, sessionId);
        if(o_Exists == null){
            // Return data back to ajax calling
            //Classing o_Classing = classingService.getAClassingByCourseAndClassId(courseId, i_classId);
            //int classingId = o_Classing.getId();
                //coursingService.editACoursing(id, teacherId, courseId, sessionId, i_classId, classingId);
            int iTotalTimeCreditsPerDay = 0;
            List<CourseTeacherClassRaw> oListCourseClass = coursingService.listCourseTeacherClassById(id);
            if(oListCourseClass != null)
            {
                for(CourseTeacherClassRaw CourseClass : oListCourseClass)
                {
                    //iTotalTimeCreditsPerDay += 1;
                    //classesService.getById();
                }
            }
            coursingService.editACoursing(id, teacherId, courseId, sessionId, a_classIds);
            return new Response(true, "Edit successfully.", 1);
        }else
        {
            return new Response(false, "This record has been existed.", 0);
        }
    }
}
